package com.iotechn.unimall.app.api.plantgoods;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.domain.PlantGoodsOrderDO;
import com.iotechn.unimall.data.mapper.PlantGoodsOrderMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlantGoodsOrderServiceImpl implements PlantGoodsOrderService {

    @Autowired
    private PlantGoodsOrderMapper plantGoodsOrderMapper;


    public Object getGoodsById(Long goodsId) throws Exception {
        PlantGoodsOrderDO plantGoodsDO = plantGoodsOrderMapper.selectById(goodsId);
        return plantGoodsDO;

    }

    /**
     * 按照时间倒序查询最新20条
     *
     * @return
     * @throws Exception
     */
    @Override
    public Object todayGoods() throws Exception {
        Wrapper<PlantGoodsOrderDO> wrapper = new EntityWrapper<PlantGoodsOrderDO>();
        wrapper.eq("status", 0);
        Integer page = 1;
        Integer limit = 5;
        //时间倒序
        wrapper.orderBy("gmt_create", false);
        Integer count = plantGoodsOrderMapper.selectCount(wrapper);
        List<PlantGoodsOrderDO> PlantGoodsDOS = plantGoodsOrderMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return PlantGoodsDOS;//plantGoodsMapper.getTodayGoodsIndex();
    }

    @Override
    public Object getTodayGoods(Integer page, Integer limit, Long areaId, Long categoryId, String goodName, Double sPrice, Double ePrice) throws Exception {

        Wrapper<PlantGoodsOrderDO> wrapper = new EntityWrapper<PlantGoodsOrderDO>();
        wrapper.eq("status", 0);
        if (areaId != null) {
            wrapper.eq("area_id", areaId);
        }
        if (categoryId != null) {
            wrapper.eq("category_id", categoryId);
        }
        if (goodName != null) {
            wrapper.like("title", goodName);
        }
        if (sPrice != null && ePrice != null) {
            wrapper.between("price", sPrice, ePrice);
        }
        wrapper.orderBy("gmt_create", false);
        Integer count = plantGoodsOrderMapper.selectCount(wrapper);
        List<PlantGoodsOrderDO> PlantGoodsDOS = plantGoodsOrderMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(PlantGoodsDOS, page, limit, count);
    }
}
